package com.dhcc.sds.base.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.sys.dao.CdcProductMapper;
import com.dhcc.cdc.sys.entity.CdcProductEntity;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.result.node.ZTreeNode;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sds.base.dao.*;
import com.dhcc.sds.base.entity.*;
import com.dhcc.sds.base.service.ISdsIndexHeaderService;
import com.dhcc.sds.base.service.ISdsSecondaryHeaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 单病种指标统计配置服务层接口实现
 *
 * @ClassName: SdsIndexHeaderServiceImpl
 * @Description: TODO
 * @author zhangdc
 * @date 2021-09-03 11:14:53
 */
@Service("sdsIndexHeaderService")
public class SdsIndexHeaderServiceImpl extends BaseServiceImpl<SdsIndexHeaderMapper, SdsIndexHeaderEntity> implements ISdsIndexHeaderService {

    @Resource
    private CdcProductMapper productMapper;
    @Resource
    private SdsQcentityMapper sdsQcentityMapper;
    @Resource
    private SdsIndexFieldMapper sdsIndexFieldMapper;
    @Resource
    private SdsQcformItemMapper sdsQcformItemMapper;

    @Override
    public List<SdsIndexHeaderEntity> page(Page<SdsIndexHeaderEntity> page, Map<String, Object> map) {
        return this.baseMapper.page(page, map);
    }

    @Override
    public List<SdsIndexHeaderEntity> list(Map<String, Object> map) {
        return this.baseMapper.list(map);
    }

    @Override
    public List<ZTreeNode> getIndexHeaderTree(Long entityId, Integer indexType) {
        List<ZTreeNode> tree = this.baseMapper.tree(entityId, indexType);
        return tree;
    }

    @Override
    public List<Map<String, Object>> selectIndexHeader(Map<String, Object> params) {
        return this.baseMapper.selectIndexHeader(params);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importIndexHeader(List<List<Object>> indexAll, Long entityId) {
        for (int i = 1; i < indexAll.size(); i++){
            List<Object> ls = indexAll.get(i);
            String entityAbbrev = ls.get(0).toString();
            Integer indexType = CommonUtil.equals(ls.get(1),"合计指标")?1:2;
            Integer headerType = CommonUtil.equals(ls.get(2),"父指标")?1:2;
            String title = ls.get(3).toString();
            String field = ls.get(4).toString();
            String parentName = ls.get(5).toString();
            String express = ls.get(6).toString();
            String btResume = ls.get(7).toString();
            Integer sort = Integer.valueOf(ls.get(8).toString());
            Integer btIsActive = CommonUtil.equals(ls.get(9),"是")?1:0;

            //获取父指标ID
            Long parentId = null;
            SdsIndexHeaderEntity pIndexHeaderEntity = new SdsIndexHeaderEntity();
            pIndexHeaderEntity.setEntityId(entityId);
            pIndexHeaderEntity.setTitle(parentName);
            pIndexHeaderEntity.setIndexType(indexType);
            pIndexHeaderEntity = this.baseMapper.selectOne(pIndexHeaderEntity);
            if (pIndexHeaderEntity != null){
                parentId = pIndexHeaderEntity.getId();
            }

            // 判断指标是否重复
            SdsIndexHeaderEntity sdsIndexHeaderEntity = new SdsIndexHeaderEntity();
            sdsIndexHeaderEntity.setEntityId(entityId);
            sdsIndexHeaderEntity.setTitle(title);
            sdsIndexHeaderEntity.setIndexType(indexType);
            sdsIndexHeaderEntity.setHeaderType(headerType);
            sdsIndexHeaderEntity = this.baseMapper.selectOne(sdsIndexHeaderEntity);
            if (sdsIndexHeaderEntity == null){
                sdsIndexHeaderEntity = new SdsIndexHeaderEntity();
                sdsIndexHeaderEntity.setEntityId(entityId);
                sdsIndexHeaderEntity.setParentId(parentId);
                sdsIndexHeaderEntity.setIndexType(indexType);
                sdsIndexHeaderEntity.setHeaderType(headerType);
                sdsIndexHeaderEntity.setTitle(title);
                sdsIndexHeaderEntity.setField(field);
                sdsIndexHeaderEntity.setSort(sort);
                sdsIndexHeaderEntity.setBtIsActive(btIsActive);
                sdsIndexHeaderEntity.setExpress(express);
                sdsIndexHeaderEntity.setBtResume(btResume);
                sdsIndexHeaderEntity.init();
                sdsIndexHeaderEntity.insert();
            }else {
                sdsIndexHeaderEntity.setIndexType(indexType);
                sdsIndexHeaderEntity.setParentId(parentId);
                sdsIndexHeaderEntity.setField(field);
                sdsIndexHeaderEntity.setBtIsActive(btIsActive);
                sdsIndexHeaderEntity.setSort(sort);
                sdsIndexHeaderEntity.setHeaderType(headerType);
                sdsIndexHeaderEntity.setExpress(express);
                sdsIndexHeaderEntity.setBtResume(btResume);
                sdsIndexHeaderEntity.init();
                sdsIndexHeaderEntity.updateAllColumnById();
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importIndexItem(List<List<Object>> indexItemAll, Long entityId) {
        for (int i = 1; i < indexItemAll.size(); i++) {
            List<Object> ls = indexItemAll.get(i);
            String entityAbbrev = ls.get(0).toString();
            Integer indexType = CommonUtil.equals(ls.get(1),"合计指标")?1:2;
            String title = ls.get(2).toString();
            String itmeCode = ls.get(3).toString();
            String code = ls.get(4).toString();

            // 获取指标ID
            Long indexHeaderId = null;
            SdsIndexHeaderEntity sdsIndexHeaderEntity = new SdsIndexHeaderEntity();
            sdsIndexHeaderEntity.setEntityId(entityId);
            sdsIndexHeaderEntity.setTitle(title);
            sdsIndexHeaderEntity.setIndexType(indexType);
            sdsIndexHeaderEntity = this.baseMapper.selectOne(sdsIndexHeaderEntity);
            if (sdsIndexHeaderEntity != null){
                indexHeaderId = sdsIndexHeaderEntity.getId();
            }

            // 获取项目ID
            Long itemId = null;
            SdsQcformItemEntity item = new SdsQcformItemEntity();
            item.setEntityId(entityId);
            item.setBtCode(code);
            item = sdsQcformItemMapper.selectOne(item);
            if (item != null){
                itemId = item.getId();
            }


            // 判断项目对照是否重复
            SdsIndexFieldEntity sdsIndexFieldEntity = new SdsIndexFieldEntity();
            sdsIndexFieldEntity.setEntityId(entityId);
            sdsIndexFieldEntity.setIndexHeaderId(indexHeaderId);
            sdsIndexFieldEntity.setItemCode(itmeCode);
            sdsIndexFieldEntity = sdsIndexFieldMapper.selectOne(sdsIndexFieldEntity);
            if (sdsIndexFieldEntity == null){
                sdsIndexFieldEntity = new SdsIndexFieldEntity();
                sdsIndexFieldEntity.setEntityId(entityId);
                sdsIndexFieldEntity.setIndexHeaderId(indexHeaderId);
                sdsIndexFieldEntity.setItemCode(itmeCode);
                sdsIndexFieldEntity.setItemId(itemId);
                sdsIndexFieldEntity.insert();
            }else {
                sdsIndexFieldEntity.setItemCode(itmeCode);
                sdsIndexFieldEntity.setItemId(itemId);
                sdsIndexFieldEntity.updateAllColumnById();
            }
        }

    }

    @Override
    public List<SdsIndexHeaderEntity> getParentIndexHeader(Map<String, Object> map) {
        return this.baseMapper.selectParentIndexHeader(map);
    }

    @Override
    public JSONObject getAnalysisCaseDic(Map<String, String> params) {
        JSONObject obj = new JSONObject();
        Wrapper<SdsIndexHeaderEntity> wrapper = new EntityWrapper<SdsIndexHeaderEntity>();
        wrapper.eq("analysis_status", 1).eq("entity_id", Long.parseLong(params.get("qcEntity")));
        List<SdsIndexHeaderEntity> list = this.baseMapper.selectList(wrapper);
        obj.put("chart", list);
        return obj;
    }

    @Override
    public String getTitleByField(Long entityId, Integer indexType, String field) {
        SdsIndexHeaderEntity sdsIndexHeaderEntity = new SdsIndexHeaderEntity();
        sdsIndexHeaderEntity.setEntityId(entityId);
        sdsIndexHeaderEntity.setIndexType(indexType);
        sdsIndexHeaderEntity.setField(field);
        SdsIndexHeaderEntity sdsIndexHeaderEntity1 = this.baseMapper.selectOne(sdsIndexHeaderEntity);
        if (!StringUtils.isEmpty(sdsIndexHeaderEntity1)){
            return sdsIndexHeaderEntity1.getTitle();
        }else {
            return "";
        }
    }
}
